package com.exsun.ex1077.ui.activity.mine

import android.os.Bundle
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.PhoneUtils
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import com.exsun.ex1077.R
import com.exsun.ex1077.databinding.ActivityMineBinding
import com.exsun.ex1077.extend.logout
import com.exsun.ex1077.model.MenuItem
import com.exsun.ex1077.ui.activity.feedback.FeedbackActivity
import com.exsun.ex1077.ui.activity.login.LoginActivity
import com.exsun.ex1077.ui.activity.modifypwd.ModifyPwdActivity
import com.exsun.ex1077.ui.activity.privacy.PrivacyActivity
import com.zhangping.fastjetpack.base.BaseActivity
import com.zhangping.fastjetpack.extend.*
import com.zhangping.fastjetpack.extend.base.parseState
import com.zhangping.fastjetpack.network.upgrade.pgyer.PgyerCheckUpdate
import kotlinx.android.synthetic.main.activity_mine.*

/**
 * 描述：我的页面
 * 作者：ZhangPing
 * 创建时间：2022/8/4 14:30
 */
class MineActivity: BaseActivity<MineViewModel, ActivityMineBinding>() {

    override fun barBgColor() = R.color.transparent
    override fun canBack() = true
    override fun backIcon() = R.drawable.back_white
    override fun title() = "我的"
    override fun titleColor() = R.color.white
    override fun statusBarDarkFont() = false

    //客服电话
    private var serviceTel: String? = null

    private val menus = arrayListOf(
        MenuItem("0", "联系客服", R.drawable.icon_lxkf),
        MenuItem("1", "意见反馈", R.drawable.icon_yjfk),
        MenuItem("2", "检查更新", R.drawable.icon_jcgx, "当前版本:" + AppUtils.getAppVersionName()),
        MenuItem("3", "修改密码", R.drawable.icon_xgmm),
        MenuItem("4", "清除缓存", R.drawable.icon_qchc, getAppCacheSize()),
        MenuItem("5", "隐私政策", R.drawable.icon_yszc),
        MenuItem("6", "退出登录", R.drawable.icon_tcdl),
    )

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        super.initView(savedInstanceState)
        mViewModel.apply {
            username.set(getCacheString("name"))
            companyName.set(getCacheString("companyName"))
            photo.set(getCacheString("photo"))
        }
        menuList.linear().setup {
            addType<MenuItem>(R.layout.list_item_mine_menu)
            onClick(R.id.itemRoot){
                when(modelPosition){
                    0 -> {
                        //联系客服
                        serviceTel?.let {
                            PhoneUtils.dial(it)
                        }
                    }
                    1 -> {
                        //意见反馈
                        toActivity<FeedbackActivity>()
                    }
                    2 -> {
                        //检查更新
                        PgyerCheckUpdate.checkUpdate(this@MineActivity, "当前已是最新版本")
                    }
                    3 -> {
                        //修改密码
                        toActivity<ModifyPwdActivity>()
                    }
                    4 -> {
                        //清除缓存
                        showConfirm("是否清除缓存？"){
                            clearAppCache()
                            "缓存清除成功".showToast()
                            (mutable[4] as MenuItem).subTitle = getAppCacheSize()
                            notifyItemChanged(4)
                        }
                    }
                    5 -> {
                        //隐私政策
                        toActivity<PrivacyActivity>()
                    }
                    6 -> {
                        //退出登录
                        showConfirm("确定要退出登录吗？"){
                            logout(true)
                            finishToActivity<LoginActivity>()
                        }
                    }
                }
            }
        }.models = menus

        head.setOnClickListener {
            selectPhoto(1){
                mViewModel.headUpload(it[0])
            }
        }

        mViewModel.loadData()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.serviceTelResult.observe(this){
            parseState(it, {
                serviceTel = it?.phone
            })
        }
    }

}